const File = require("../models/fileModel");

module.exports.findAll = async (current = 1, pageSize = 10, searchParams,order = ["create_time", "DESC"]) => {
    
    // if (searchParams.title) {
    //     searchParams.title = { [Op.like]: "%" + searchParams.title + "%" };
    // }
    // if (searchParams.text) {
    //     searchParams.text = { [Op.like]: "%" + searchParams.text + "%" };
    // }
    
    const data = await File.findAndCountAll({
        limit: pageSize,
        offset: (current - 1) * pageSize,
        where:searchParams,
        order: [order],
    });
    data.current = current;
    data.pageSize = pageSize;
    data.total = data.count;
    data.totalPage = Math.ceil(data.count / pageSize);
    return data;
};

module.exports.findById = async (id) => {
    return await File.findByPk(id);
};
module.exports.create = async (file) => {
    return await File.create(file);
};
module.exports.update = async (file) => {
    let id = file.id;
    delete file.id;
    return await File.update(file, { where: { id } });
};

module.exports.delete = async (id) => {
    return await File.destroy({ where: { id: id } });
};